package com.hspedu.newData.dmsxl.动态规划.基础;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-06 
 * @Version: 1.0
 */

public class LC139单词拆分 {

    public boolean wordBreak(String s, List<String> wordDict) {
        int len = s.length();
        boolean[] dp = new boolean[len + 1];
        Set<String> wordDictSet = new HashSet(wordDict);
        dp[0] = true;
        StringBuilder sb = new StringBuilder(s);
        for (int i = 1; i <= len; i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDictSet.contains(sb.substring(j,i))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[len];
    }
    @Test
    public void test() {
        // 测试代码
        ArrayList<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("aaa");
        wordBreak("aaaaaaa",list);
    }

}
